package com.anglireports.report.response.util;

import org.joda.time.DateTime;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {

	public static String datetimeFormat = "yyyy-MM-dd HH:mm:ss";

	public static String timeFormat = "yyyyMMddHHmmss";

	public static String dateFormat = "yyyyMMdd";

	public static String simpleFormat = "yyyyMMdd";

	public static String dateFormatPay = "yyyy-MM-dd";

	public static String dateFormat2 = "yyyy/MM/dd";

	public static String dateFormat3 = "MM/dd";

	/**
	 * 将字符串转换成yyyy-MM-dd格式日期时间
	 * 
	 * @param date1
	 * @return
	 */
	public static Timestamp str2Date(String str) {
		Timestamp date = null;
		try {
			date = new Timestamp(new SimpleDateFormat(dateFormat).parse(str)
					.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return date;
	}

	public static Date timeToDate(String str) {
		Timestamp date = null;
		try {
			date = new Timestamp(new SimpleDateFormat(timeFormat).parse(str)
					.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return date;
	}

	public static String getTime(Date date) {
		return new SimpleDateFormat(timeFormat).format(date);
	}

	public static String simpleFormat(Date date) {
		return new SimpleDateFormat(simpleFormat).format(date);
	}

	public static String getDate() {
		return new SimpleDateFormat(dateFormat).format(new Date());
	}

	public static String getDatePay() {
		return new SimpleDateFormat(dateFormatPay).format(new Date());
	}

	public static String getTime() {
		return new SimpleDateFormat(timeFormat).format(new Date());
	}

	public static String formatTime() {
		return new SimpleDateFormat(datetimeFormat).format(new Date());
	}

	public static String formatTime(Date date) {
		return new SimpleDateFormat(datetimeFormat).format(date);
	}

	/**
	 * 将日期时间转换成 yyyy-MM-dd HH:mm:ss类型字符串
	 * 
	 * @param date1
	 * @return
	 */
	public static String datetimeToStr(Timestamp datetime) {
		return new SimpleDateFormat(datetimeFormat).format(datetime);
	}

	public static String getEndDatetime(String startDatetime)
			throws ParseException {
		// 通过startDatetime生成当前结束时间
		String endDatetime = startDatetime.substring(0, 13);
		endDatetime += ":59:59";

		return endDatetime;
	}

	public static String getNextStartDatetime(String startDatetime)
			throws ParseException {
		// 通过startDatetime生成下次开始时间
		Calendar calendar = Calendar.getInstance();// 获取系统当前时间
		Date utilDatetime = new Date(new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss").parse(startDatetime).getTime());
		calendar.setTime(utilDatetime);

		calendar.add(Calendar.HOUR, +1);// 向后推一个小时
		String nextStartDatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(calendar.getTime());

		return nextStartDatetime;
	}

	/**
	 * 日期类型转字符串类型
	 * 
	 * @param date
	 * @param dateFormat
	 * @return
	 */
	public static String dateToString(Date date, String dateFormat) {
		if (date == null)
			return "";
		try {
			return new SimpleDateFormat(dateFormat).format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 日期类型转字符串类型，默认返回yyyy-MM-dd HH:mm:ss格式
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		return dateToString(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static long date2timestamp(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		long time = 0l;
		try {
			time = dateFormat.parse(DateUtil.formatTime(date)).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	public static long date2timestamp(String date, String formatStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
		long time = 0l;
		try {
			time = dateFormat.parse(date).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	public static String ndaysDate(String strdate, int n,String dateFormat) {
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		String nstrdate = "";
		try {
			Date myDate = formatter.parse(strdate);
			Calendar c = Calendar.getInstance();
			c.setTime(myDate);
			c.add(Calendar.DAY_OF_MONTH, n);
			myDate = c.getTime();
			nstrdate = formatter.format(myDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nstrdate;
	}

	public static void main(String[] args){
		System.out.print(ndaysDate("2019/12/10",2, dateFormat2));

	}

	public static String formatDate(String strdate, String sourceFormat, String targetFormat) {
		SimpleDateFormat formatter = new SimpleDateFormat(sourceFormat);
		SimpleDateFormat formatter2 = new SimpleDateFormat(targetFormat);
		String nstrdate = "";
		try {
			Date myDate = formatter.parse(strdate);
			nstrdate = formatter2.format(myDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nstrdate;
	}


	public static String isoStrToTargetStr(String isoStr, String targetDateFormat){
		DateTimeFormatter isoformat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
		DateTime dt= isoformat.parseDateTime(isoStr).withZone(DateTimeZone.UTC);
		DateTimeFormatter format = DateTimeFormat.forPattern(targetDateFormat);
		return dt.toString(format);
	}

	public static String strStrToiso(String str, int nDay){
		DateTimeFormatter isoformat = DateTimeFormat.forPattern("yyyyMMdd").withZone(DateTimeZone.UTC);
		str = ndaysDate(str, nDay ,"yyyyMMdd");
		DateTime dt= isoformat.parseDateTime(str);
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		return dt.toString(format);
	}

	public static boolean compareTime(String time1, String time2){
		DateTimeFormatter isoformat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
		DateTime dt1= isoformat.parseDateTime(time1).withZone(DateTimeZone.UTC);
		DateTime dt2= isoformat.parseDateTime(time2).withZone(DateTimeZone.UTC);
		return dt1.isBefore(dt2);
	}


	public static int getDiffDay (String time1, String time2){
		DateTimeFormatter isoformat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
		DateTime dt1= isoformat.parseDateTime(time1).withZone(DateTimeZone.UTC);
		DateTime dt2= isoformat.parseDateTime(time2).withZone(DateTimeZone.UTC);
		return (int)((dt2.getMillis() - dt1.getMillis()) / (1000 * 60 * 60 *24));
	}
}
